package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicPlaceBuildingDTO;
import com.iwomy.secureplat.platform.entity.BasicInstallLocationEntity;
import com.iwomy.secureplat.platform.entity.BasicPlaceBuildingEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.service.BasicPlaceBuildingService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 建筑物管理
 *
 * @author pig
 * @date 2023-11-30 13:54:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicPlaceBuilding")
@Tag(description = "basicPlaceBuilding", name = "建筑物管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicPlaceBuildingController {

	private final BasicPlaceBuildingService basicPlaceBuildingService;

	private final BasicInstallLocationService basicInstallLocationService;

	/**
	 * 分页查询
	 *
	 * @param page                  分页对象
	 * @param basicPlaceBuildingDTO 建筑物管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_view')" )
	public R getBasicPlaceBuildingPage(@ParameterObject Page page, @ParameterObject BasicPlaceBuildingDTO basicPlaceBuildingDTO) {
		return R.ok(basicPlaceBuildingService.getBasicPlaceBuildingPage(page, basicPlaceBuildingDTO));
	}

	/**
	 * 分页查询
	 *
	 * @param basicPlaceBuilding 建筑物管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_view')")
	public R getPlaceBuildinglList(@ParameterObject BasicPlaceBuildingEntity basicPlaceBuilding) {
		return R.ok(basicPlaceBuildingService.pageByScope(basicPlaceBuilding));
	}


	/**
	 * 通过id查询建筑物管理
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicPlaceBuildingService.getById(id));
	}

	/**
	 * 新增建筑物管理
	 *
	 * @param basicPlaceBuilding 建筑物管理
	 * @return R
	 */
	@Operation(summary = "新增建筑物管理", description = "新增建筑物管理")
	@SysLog("新增建筑物管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_add')")
	public R save(@RequestBody BasicPlaceBuildingEntity basicPlaceBuilding) {
		BasicPlaceBuildingEntity wrapperEntity = new BasicPlaceBuildingEntity();
		wrapperEntity.setBuildingName(basicPlaceBuilding.getBuildingName());
		wrapperEntity.setDeptId(basicPlaceBuilding.getDeptId());
		LambdaQueryWrapper<BasicPlaceBuildingEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);

		BasicPlaceBuildingEntity one = basicPlaceBuildingService.getOne(wrapper);
		if (ObjectUtil.isNotEmpty(one)) {
//			throw new PlatformException("建筑物已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.PLACEBUILDING_EXIST));
		}
		return basicPlaceBuildingService.newlyAdded(basicPlaceBuilding);
	}

	/**
	 * 修改建筑物管理
	 *
	 * @param basicPlaceBuilding 建筑物管理
	 * @return R
	 */
	@Operation(summary = "修改建筑物管理", description = "修改建筑物管理")
	@SysLog("修改建筑物管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_edit')")
	public R updateById(@RequestBody BasicPlaceBuildingEntity basicPlaceBuilding) {
		BasicPlaceBuildingEntity wrapperEntity = new BasicPlaceBuildingEntity();
		wrapperEntity.setBuildingName(basicPlaceBuilding.getBuildingName());
		wrapperEntity.setDeptId(basicPlaceBuilding.getDeptId());
		LambdaQueryWrapper<BasicPlaceBuildingEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);

		BasicPlaceBuildingEntity one = basicPlaceBuildingService.getOne(wrapper);
		if (ObjectUtil.isNotEmpty(one)&&!one.getId().equals(basicPlaceBuilding.getId())) {
//			throw new PlatformException("建筑物已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.PLACEBUILDING_USED_MODIFY));
		}
		return R.ok(basicPlaceBuildingService.updateById(basicPlaceBuilding));
	}

	/**
	 * 通过id删除建筑物管理
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除建筑物管理", description = "通过id删除建筑物管理")
	@SysLog("通过id删除建筑物管理")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_del')")
	public R removeById(@RequestBody Long[] ids) {
		long count = basicInstallLocationService.count(Wrappers.<BasicInstallLocationEntity>lambdaQuery()
				.in(BasicInstallLocationEntity::getBuildingId, ids));
		if (count > 0) {
//			return R.failed("已绑定安装点无法删除，请先解除绑定");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.PLACEBUILDING_USED_DELETE));
		}
		return R.ok(basicPlaceBuildingService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicPlaceBuilding 查询条件
	 * @param ids                导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceBuilding_export')")
	public List<BasicPlaceBuildingEntity> export(BasicPlaceBuildingEntity basicPlaceBuilding, Long[] ids) {
		return basicPlaceBuildingService.list(Wrappers.lambdaQuery(basicPlaceBuilding).in(ArrayUtil.isNotEmpty(ids), BasicPlaceBuildingEntity::getId, ids));
	}
}